package extraction.html.chase;

import org.htmlparser.util.ParserException;
import db.ChaseDAO;

import java.sql.SQLException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.ObjectOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author Nathan Petryk (nathanp@indeed.com)
 */
public class ChaseExtractor {

    private MessageDigest hasher;
    private ChaseDAO cdao;


    public ChaseExtractor() {
        try {
            cdao = new ChaseDAO("jdbc:mysql://localhost/pers", "persuser", "1234");
            hasher = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            hasher = null;
            // log error
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void extract() {
        try {

            AccountSummary acnt = ChaseAccountScreenParser.extractAccountSummary(
                    "//home//nathanp//jtml//resources//samplepages//MyAccounts.aspx");
            AccountTransaction[] transactions = ChaseTransactionScreenParser.extractTransactions(
                    "//home//nathanp//jtml//resources//samplepages//AccountDetails.aspx");

            int accntId = cdao.insertAccount(acnt);

            for(AccountTransaction trans : transactions) {
                cdao.insertTransaction(trans, accntId);
            }
        } catch (ParserException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private int hashObject(Object obj) throws IOException {

        byte[] data;

        {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();
        bos.close();
        data = bos.toByteArray();    
        }

        hasher.digest(data);

        return -1;
    }

    public static void main(String[] args) {
        ChaseExtractor cext = new ChaseExtractor();
    }

}
